﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace LIB
{
    class PublicConst
    {
        /// <summary>
        /// 如果web.congfig中没有connstr，则使用程序代码中内嵌的。
        /// 如果有则判断useConfig是否有1，为1则使用config中连接串，否则使用程序代码中内嵌的。
        /// 这样的好处是：更换数据库时候既可以方便的通过配置来改；有安全性要求时候还可以通过重新发布lib.dll来改。
        /// </summary>
        static string connstr = "server = .; integrated security = sspi; database = master";
        static string connstr2 = "";
        private static string ConnectStr = "";
        private System.Configuration.Configuration _configHelp = null;

        /// <summary>
        /// 获取链接字符串的函数
        /// </summary>
        /// <returns></returns>
        public string getConnstr()
        {
            string useConfig = System.Configuration.ConfigurationSettings.AppSettings["useConfig"];
            if (useConfig == "1")
            {
                _configHelp = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
                ConnectStr = _configHelp.ConnectionStrings.ConnectionStrings["connstr"].ConnectionString;
            }
            else ConnectStr = connstr;
            
            return ConnectStr;
        }

        public string getConnstr(string connstrName)
        {
            string useConfig = System.Configuration.ConfigurationSettings.AppSettings["useConfig"];
            if (useConfig == "1")
            {
                _configHelp = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
                ConnectStr = _configHelp.ConnectionStrings.ConnectionStrings[connstrName].ConnectionString;
            }
            else ConnectStr = connstr2;

            return ConnectStr;
        }

    }
}
